//
//  LEServiceController.m
//  LoveExpressBusiness
//
//  Created by 刘甲奇 on 2017/5/10.
//  Copyright © 2017年 DFH. All rights reserved.
//

#import "LEServiceController.h"
#import "LEGoodStaffCell.h"
#import "LEServiceCell.h"
#import "LEStaffModel.h"

#import "LEStaffDetialController.h"
#import "LESendRankingController.h"
#import "LEGradeRankingController.h"
#import "LECommitListController.h"
#import "LEReplyListController.h"
#import "MakerTools.h"
#import "LEStoreModel.h"

static NSString *const LEGoodStaffCellId = @"LEGoodStaffCell";
static NSString *const LEServiceCellId = @"LEServiceCell";

@interface LEServiceController ()

// 图片数据源
@property(nonatomic,strong)NSArray* imageArray;
// 标题数据源
@property(nonatomic,strong)NSArray* titleArray;
/**
 订单最多的员工
 */
@property(nonatomic,strong)LEStaffModel *orderStaff;
/**
 业绩最多的员工
 */
@property(nonatomic,strong)LEStaffModel *scoreStaff;

@end

@implementation LEServiceController

- (void)setupUI
{
    [super setupUI];
    self.title = @"服务管理";
    self.tableView.backgroundColor = LEBackgroundColor;
    
    self.tableView.bounces = NO;
    self.tableView.sectionFooterHeight = 8;
    self.tableView.sectionHeaderHeight = 0;
    [self.tableView registerNib:[UINib nibWithNibName:@"LEGoodStaffCell" bundle:nil] forCellReuseIdentifier:LEGoodStaffCellId];
    [self.tableView registerNib:[UINib nibWithNibName:@"LEServiceCell" bundle:nil] forCellReuseIdentifier:LEServiceCellId];
}
-(void)viewWillAppear:(BOOL)animated
{
    [self.tabBarController.tabBar setHidden:NO];
}
- (void)loadData
{
    NSArray *imageArr = @[@"service_sendDescend",@"service_moneyDescend",@"service_commit",@"service_reply"];
    _imageArray = imageArr;
    
    NSArray *titleArr = @[@"配送排名",@"绩效排名",@"评价管理",@"投诉处理"];
    _titleArray = titleArr;
    [self requestRefreshData];
}
#pragma mark- 网络请求
- (void)requestRefreshData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@"1" forKey:@"curpage"];
    [dict setObject:@"10" forKey:@"page"];
    LEStoreModel *model = [MakerTools readFromSandBox:Store_Info];
    [dict setObject:model.store_id forKey:@"store_id"];
    
    //请求送单最多
    [SVProgressHUD showWithStatus:@"正在加载中"];
    __weak typeof (self) weakSelf = self;
    [API requestAppendKey:ServiceOrderRankUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        
        id datas = successCode[@"datas"];
        NSString *error = nil;
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            error = datas[@"error"];
        }
        if (error)
        {
            [SVProgressHUD showErrorWithStatus:error];
        }else
        {
            LEStaffModel *staffModel1 = [LEStaffModel mj_objectWithKeyValues:datas[0]];
            staffModel1.isOrderMost = @"1";
            self.orderStaff = staffModel1;
            [weakSelf.tableView reloadData];
        }
        
    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
    
    //请求业绩最多
    [API requestAppendKey:ServiceScoreRankUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        
        id datas = successCode[@"datas"];
        NSString *error = nil;
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            error = datas[@"error"];
        }
        if (error)
        {
            [SVProgressHUD showErrorWithStatus:error];
        }else
        {
            LEStaffModel *staffModel1 = [LEStaffModel mj_objectWithKeyValues:datas[0]];
            staffModel1.isOrderMost = @"0";
            self.scoreStaff = staffModel1;
            [weakSelf.tableView reloadData];
        }
        
    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];

}
#pragma mark- tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }else
        return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 130;
    }
    return 55;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return 15;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
      LEGoodStaffCell *cell = [tableView dequeueReusableCellWithIdentifier:LEGoodStaffCellId forIndexPath:indexPath];
        if (indexPath.row == 0)
        {
            if (self.orderStaff)
            {
                cell.model = self.orderStaff;
            }
        }
        if (indexPath.row == 1)
        {
            if (self.scoreStaff)
            {
               cell.model = self.scoreStaff;
            }
        }
      return cell;
    }
    LEServiceCell *cell = [tableView dequeueReusableCellWithIdentifier:LEServiceCellId forIndexPath:indexPath];
    NSString *imageStr = self.imageArray[indexPath.row];
    cell.icon.image = [UIImage imageNamed:imageStr];
    cell.titleLabel.text = self.titleArray[indexPath.row];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        //员工详情
        LEStaffDetialController *staffDetial = [[LEStaffDetialController alloc] init];
        LEStoreModel *storeModel = [MakerTools readFromSandBox:Store_Info];
        LEStaffModel *model = nil;
        if (indexPath.row == 0) {
            model = self.orderStaff;
        }else{
            model = self.scoreStaff;
        }
        staffDetial.store_id = storeModel.store_id;
        staffDetial.service_id = model.service_id;
        [self.navigationController pushViewController:staffDetial animated:YES];
    }else{
        //各种排名
        switch (indexPath.row) {
            case 0:
            {
                LESendRankingController *sendRanking = [[LESendRankingController alloc] init];
                [self.navigationController pushViewController:sendRanking animated:YES];
            }
                break;
            case 1:
            {
                LEGradeRankingController *gradeRanking = [[LEGradeRankingController alloc] init];
                [self.navigationController pushViewController:gradeRanking animated:YES];
            }
                break;
            case 2:
            {
                LECommitListController *commitList = [[LECommitListController alloc] init];
                [self.navigationController pushViewController:commitList animated:YES];
            }
                break;
            case 3:
            {
                LEReplyListController *relpyList = [[LEReplyListController alloc] init];
                [self.navigationController pushViewController:relpyList animated:YES];
            }
                break;
                
            default:
                break;
        }
    }
}

@end










